package com.happycat.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.happycat.base.PageParams;
import com.happycat.base.R;
import com.happycat.console.model.CatGift;
import com.happycat.console.model.CatGiftBox;
import com.happycat.console.service.ICatGiftBoxService;
import com.happycat.console.service.ICatGiftService;
import com.happycat.mobile.dto.CatGiftBoxQueryDto;
import com.happycat.mobile.dto.CatGiftQueryDto;
import com.happycat.mobile.dto.UserGiveGiftSaveDto;
import com.happycat.mobile.service.UserGiftDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

/**
 * 礼品Controller
 *
 * @author jsonbin
 * @date 2020-09-07
 */
@RestController
@RequestMapping("/cat/gift" )
@Api(tags = "API-礼物功能", value = "V1.0")
public class CatGiftController {
    @Autowired
    private ICatGiftService catGiftService;

    @Autowired
    private ICatGiftBoxService catGiftBoxService;

    @Autowired
    private UserGiftDetailService userGiftDetailService;


    @PostMapping(value = "/giftPage")
    @ApiOperation(value = "礼物列表-分页")
    public R<IPage<CatGift>> giftPage(@RequestBody PageParams<CatGiftQueryDto> dto){
        // 处理参数
        IPage<CatGift> page = dto.buildPage();

        if(dto.getModel().getQueryType() == 0 || dto.getModel().getQueryType()==1 || dto.getModel().getQueryType()==2){
            return R.success(catGiftService.page(page, new QueryWrapper<CatGift>()
                    .eq("gift_type", dto.getModel().getQueryType())
                    .eq("position", dto.getModel().getPosition())
                    .eq("status", 1)));
        }else if(dto.getModel().getQueryType() == 9){
            return R.success(catGiftService.page(page, new QueryWrapper<CatGift>()
                    .in("gift_type", 0, 1)
                    .eq("position", dto.getModel().getPosition())
                    .eq("status", 1)));
        }else{
            return R.success(page);
        }
    }

    @PostMapping(value = "/getAllGift")
    @ApiOperation(value = "礼物列表-全部")
    public R<List<CatGift>> getAllGift(@RequestBody CatGiftQueryDto dto){
        if(dto.getQueryType() == 0 || dto.getQueryType()==1 || dto.getQueryType()==2){
            return R.success(catGiftService.list(new QueryWrapper<CatGift>()
                    .eq("status", 1)
                    .eq("gift_type", dto.getQueryType())
                    .eq("position", dto.getPosition())
                    .orderByAsc("food_number")
            ));
        }else if(dto.getQueryType() == 9){
            return R.success(catGiftService.list(new QueryWrapper<CatGift>()
                    .eq("status", 1)
                    .in("gift_type", 0, 1)
                    .eq("position", dto.getPosition())
                    .orderByAsc("food_number")
            ));
        }else{
            return R.success(Arrays.asList());
        }
    }

    @PostMapping(value = "/giftBoxPage")
    @ApiOperation(value = "惊喜盒列表-分页")
    public R<IPage<CatGiftBox>> giftBoxPage(@RequestBody PageParams<CatGiftBoxQueryDto> dto){
        // 处理参数
        IPage<CatGiftBox> page = dto.buildPage();
        //惊喜盒
        return R.success(catGiftBoxService.page(page, new QueryWrapper<CatGiftBox>().eq("status", 1)));
    }

    @PostMapping(value = "/getAllGiftBox")
    @ApiOperation(value = "惊喜盒列表-全部")
    public R<List<CatGiftBox>> getAllGiftBox(){
        //惊喜盒
        return R.success(catGiftBoxService.list(new QueryWrapper<CatGiftBox>().eq("status", 1)));
    }

//    @PostMapping(value = "/receiveFreeGift")
//    @ApiOperation(value = "领取免费礼物,该接口好像不需要，退出房间后礼物就没了")
//    public R<UserGiftDetail> receiveFreeGift(@RequestBody UserGetFreeGiftSaveDto dto){
//        return R.success(userGiftDetailService.receiveFreeGift(dto));
//    }

    @PostMapping(value = "/giveGift")
    @ApiOperation(value = "赠送礼物, 暂时只支持免费、普通、特权礼物")
    public R<Boolean> giveGift(@RequestBody UserGiveGiftSaveDto dto){
        return R.success(userGiftDetailService.giveGift(dto));
    }


//    @GetMapping(value = "/getAllPackgeGift")
//    @ApiOperation(value = "获取背包礼物-全部")
//    public R<List<CatGift>> getAllPackgeGift(){
//        return R.success(catGiftService.getAllPackgeGift());
//    }


//    @PostMapping(value = "/givePackgeGift")
//    @ApiOperation(value = "赠送背包礼物")
//    public R<Boolean> givePackgeGift(@RequestBody UserGiveGiftSaveDto dto){
//        return R.success(userGiftDetailService.givePackgeGift(dto));
//    }

}
